package com.lvcoding.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author wuyanshen
 * @discription 描述
 * @date 2021-02-25 下午1:18
 */

@Component
public class UserDetailServiceImpl implements UserDetailsService {

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User();
        if ("admin".equals(username)) {
            user.setUsername("admin");
            user.setPassword(passwordEncoder.encode("123"));
            user.setAuthorities(Arrays.asList("u:admin:view"));
        } else {
            user.setUsername("jack");
            user.setPassword(passwordEncoder.encode("123"));
            user.setAuthorities(Arrays.asList("u:common:view"));
        }
        return user;
    }
}
